<?php

class Suggestion extends AppModel {
    public $name = 'Suggestion';
    public $useTable = 'suggestion';
    
    /**
     *
     * @param type $options
     * @return type 
     */
    public function beforeSave($options = array()) {
        parent::beforeSave($options);
        $record = $this->find('first',array('conditions' => array('child' => $this->data['Suggestion']['child'],'parents' => $this->data['Suggestion']['parents'])));
        if($record)
            return false;
        return true;
    }
    
    /**
     *
     * @param type $parents
     * @return type 
     */
    public function getAllProductOfParents($parents){
        $sql = "SELECT * FROM suggestion as Suggestion,product_language as Productlanguage WHERE Suggestion.child = Productlanguage.product_id AND Suggestion.parents = ? AND Productlanguage.language_id = ?";
        $results = $this->getDataSource()->fetchAll($sql,array($parents,Configure::read('Config.language')));
        return $results;
    }
    /*
     * get lis property suggestion
     */
    public function getAllProductOfParentsView($parents){
        $sql = "SELECT Productlanguage.*, product.product_id, product.created, product.price, product.image, product.address, product.product_type , product.unit_price,
                product_type.name as producttype_name ,product.location_id
                FROM suggestion as Suggestion,product_language as Productlanguage, product_type, product
                WHERE Suggestion.child = Productlanguage.product_id 
                AND product.product_type = product_type.id
                AND product.product_id = Productlanguage.product_id
                AND Suggestion.parents = ? AND Productlanguage.language_id = ?";
        $results = $this->getDataSource()->fetchAll($sql,array($parents,Configure::read('Config.language')));
        
        App::import('model', 'Product');
        $ob = new Product();
        $array_return  = array();
        if(is_array($results)){
            foreach ($results as $value){
                $proper_product = $ob->getPropertyOfProduct($value['product']['product_type'],$value['product']['product_id']);
                if($proper_product['ProductPropertyValueArr']) unset($proper_product['ProductPropertyValueArr']);
                $value['property'] = $proper_product;
                $value['product_language'] =  $value['Productlanguage'];
                //viet add 2014-08-08
                $value['address_truelever'] = $ob->getLocationTrueLever($value['product']['location_id']);
                $array_return[] = $value;
            }
        }
        
        
        
        return $array_return;
    }
}